const express = require('express')
const router =express.Router()
const AqModel = require('../models/aq')

router.get('/find',(req,res,next)=>{
    
  AqModel.find().then(result=>{
        res.json({
            code:200,
            data:result,
            message:'aq全局查询成功',
        })
    })
})

router.post('/create',(req,res,next)=>{
    const {question,answer,answers} = req.body
    AqModel.find().then(result=>{
        const aq = new AqModel({
          aq_id:result.length?result[result.length-1].aq_id+1:0,
          question,
          answer,
          answers:answers.split(','),
          create_time:new Date(new Date().valueOf()+8*60*60*1000),
        })
        aq.save().then(result => {
            res.json({
                code:200,
                data:{
                    result
                },
                message:'aq创建完成',
            })
        })
    })
})

router.post('/update',(req,res,next)=>{
  const {aq_id,question,answer,answers} = req.body

  AqModel.updateOne({aq_id},{$set:{"question":question,"answer":answer,"answers":answers.split(',')}}).then(result=>{
        res.json({
            code:200,
            data:{
                result
            },
            message:'题目修改完成',
        })
    })
})

router.delete('/delete',(req,res,next)=>{
  const {aq_id} = req.body

  AqModel.deleteOne({aq_id}).then(result=>{
      res.json({
          code:200,
          data:{
              result
          },
          message:'删除完成',
      })
  })
})
module.exports = router